<template>
  <!-- <LoginFormTitle v-show="getShow" class="enter-x" /> -->
  <h3 class="mb-3 text-2xl font-bold text-center xl:text-3xl enter-x">{{ t('sys.login.signInFormTitle') }}</h3>
  <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef" v-show="getShow"
    @keypress.enter="handleLogin">
    <FormItem name="account" class="enter-x">
      <Input size="large" v-model:value="formData.account" :placeholder="t('sys.login.userName')" class="fix-auto-fill">
      <template #prefix>
        <UserOutlined style="color: rgb(0 0 0 / 25%)" />
      </template>
      </Input>
    </FormItem>
    <FormItem name="password" class="enter-x">
      <InputPassword size="large" visibilityToggle v-model:value="formData.password"
        :placeholder="t('sys.login.password')">
        <template #prefix>
          <LockOutlined style="color: rgb(0 0 0 / 25%)" />
        </template>
      </InputPassword>
    </FormItem>

    <ARow class="enter-x">
      <ACol :span="12">
        <FormItem>
          <Checkbox v-model:checked="rememberMe" size="small">
            {{ t('sys.login.rememberMe') }}
          </Checkbox>
        </FormItem>
      </ACol>
      <!-- <ACol :span="12">
        <FormItem :style="{ 'text-align': 'right' }">
          <Button type="link" size="small" @click="setLoginState(LoginStateEnum.RESET_PASSWORD)">
            {{ t('sys.login.forgetPassword') }}
          </Button>
        </FormItem>
      </ACol> -->
    </ARow>

    <FormItem class="enter-x">
      <Button type="primary" size="large" style="height: 40px !important" block @click="handleLogin" :loading="loading">
        {{ t('sys.login.loginButton') }}
      </Button>
      <!-- <Button size="large" class="mt-4 enter-x" block @click="handleRegister">
        {{ t('sys.login.registerButton') }}
      </Button> -->
    </FormItem>
    <!-- <ARow class="enter-x" :gutter="[16, 16]">
      <ACol :md="8" :xs="24">
        <Button block @click="setLoginState(LoginStateEnum.MOBILE)">
          {{ t('sys.login.mobileSignInFormTitle') }}
        </Button>
      </ACol>
      <ACol :md="8" :xs="24">
        <Button block @click="setLoginState(LoginStateEnum.QR_CODE)">
          {{ t('sys.login.qrSignInFormTitle') }}
        </Button>
      </ACol>
      <ACol :md="8" :xs="24">
        <Button block @click="setLoginState(LoginStateEnum.REGISTER)">
          {{ t('sys.login.registerButton') }}
        </Button>
      </ACol>
    </ARow> -->

    <!-- <Divider class="enter-x">{{ t('sys.login.otherSignIn') }}</Divider>

    <div class="flex justify-evenly enter-x" :class="`${prefixCls}-sign-in-way`">
      <GithubFilled />
      <WechatFilled />
      <AlipayCircleFilled />
      <GoogleCircleFilled />
      <TwitterCircleFilled />
    </div> -->
  </Form>
</template>
<script lang="ts" setup>
import { reactive, ref, unref, computed } from 'vue'

import { Form, Input, Button, Checkbox } from 'ant-design-vue'
import LoginFormTitle from './LoginFormTitle.vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'

import { useUserStore } from '@/store/modules/user'
import {
  LoginStateEnum,
  useLoginState,
  useFormRules,
  useFormValid,
  rsaEncryptData,
} from './useLogin'
import { useDesign } from '@/hooks/web/useDesign'
//import { onKeyStroke } from '@vueuse/core';
import { useRequest } from '@vben/hooks'
import { getPublicKey } from '@/api/sys/login'
import { useAppInject } from '@/hooks/web/useAppInject'

const FormItem = Form.Item
const InputPassword = Input.Password
const { t } = useI18n()
const { notification, createErrorModal } = useMessage()
const { prefixCls } = useDesign('login')
const userStore = useUserStore()
const { data: encryptKey } = useRequest(getPublicKey)
const { getIsMobile } = useAppInject();
const { getLoginState } = useLoginState()
const { getFormRules } = useFormRules()

const formRef = ref()
const loading = ref(false)

const formData = reactive({
  account: '18888888884',
  password: '12345678',
})

const { validForm } = useFormValid(formRef)

//onKeyStroke('Enter', handleLogin);

const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)

async function handleLogin() {
  const validformdata = await validForm()
  const keyCrepty = rsaEncryptData(validformdata.password, encryptKey.value)
  if (!validformdata) return
  try {
    loading.value = true
    const userAgent = navigator.userAgent;
    const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
    const userInfo = await userStore.login({
      username: validformdata.account,
      password: keyCrepty,
      grantType: 'password',
      clientId: 'jceClient',
      mode: 'none', //不要默认的错误提示
      isMobile: isMobile,
    })
    if (userInfo) {
      notification.success({
        message: t('sys.login.loginSuccessTitle'),
        description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.user.userName}`,
        duration: 3,
      })
    }
  } catch (error) {
    // createErrorModal({
    //   title: t('sys.api.errorTip'),
    //   content: (error as unknown as Error).message || t('sys.api.networkExceptionMsg'),
    //   getContainer: () => document.body.querySelector(`.${prefixCls}`) || document.body,
    // })
  } finally {
    loading.value = false
  }
}
</script>
